package com.xuecheng.education.common.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 *
 * 响应数据实体类
 *
 * @Author: island
 * @CreateDate: 2020/8/7 15:05
 * @ProjectName: xuecheng-education-parent
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result implements Serializable {

    private static final long serialVersionUID = -8068926359243276494L;

    /**
     * 响应码
     */
    private Integer code;
    /**
     * 响应消息
     */
    private String msg;
    /**
     * 是否正常
     */
    private Boolean flag;
    /**
     * 响应数据
     */
    private Object data;

    /**
     * 生成响应数据
     * @param resultCode 响应枚举
     */
    public Result(ResultCode resultCode){
        this.code = resultCode.getCode();
        this.msg = resultCode.getMsg();
        this.flag = resultCode.getFlag();
    }

    /**
     * 生成响应数据
     * @param resultCode 响应枚举
     * @param data       响应数据
     */
    public Result(ResultCode resultCode,Object data){
        this.code = resultCode.getCode();
        this.msg = resultCode.getMsg();
        this.flag = resultCode.getFlag();
        this.data = data;
    }
    /**
     * 快捷创建成功响应
     * @return
     */
    public static Result ok(){
        Result result = new Result();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setFlag(ResultCode.SUCCESS.getFlag());
        result.setMsg(ResultCode.SUCCESS.getMsg());
        return result;
    }

    /**
     * 快捷创建失败响应
     * @return
     */
    public static Result error(){
        Result result = new Result();
        result.setFlag(ResultCode.FAIL.getFlag());
        result.setCode(ResultCode.FAIL.getCode());
        result.setMsg(ResultCode.FAIL.getMsg());
        return result;
    }
}
